package com.liang.config;


import org.apache.coyote.OutputBuffer;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.security.PublicKey;
import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * User: XIAOLIANG
 * Date: 2021/6/19
 * Time: 11:28
 * Description: 实现基于插件的延迟队列
 */
@Configuration
public class DelayedQueueConfig {



    //延迟交换机
    public static final String DELAYED_EXCHANGE_NAME = "delayed_exchange";


    //队列
    public static final String QUEUE_NAME = "delayed_queue";

    public static final String DELAYED_ROUTINGKEY = "delayed.routingKey";

    //绑定关系



    //定义延迟交换机
    @Bean(DELAYED_EXCHANGE_NAME)
    public CustomExchange delayedExchange(){

        HashMap<String, Object> map = new HashMap<>();

        map.put("x-delayed-type","direct");

        //1 交换机的名称
        //2 交换机的类型
        // 3 是否持久化
        //4 是否自动删除
        //5 其他参数
        return new CustomExchange(DELAYED_EXCHANGE_NAME,"x-delayed-message",true,false,map);

    }


    //定义队列
    @Bean(QUEUE_NAME)
    public Queue delayed_queue(){

        return QueueBuilder.durable(QUEUE_NAME).build();

    }


    //将队列 绑定到延迟交换机上
    @Bean
    public Binding binding(
            @Qualifier(DELAYED_EXCHANGE_NAME) CustomExchange customExchange,
            @Qualifier(QUEUE_NAME) Queue queue
    ){


        return BindingBuilder.bind(queue).to(customExchange).with(DELAYED_ROUTINGKEY).noargs();

    }

}